#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<mysql.h>
#include<string.h>

struct password
{
	char id[20];
	char passwd[20];
	int flag;
};

void create(int connectfd,struct password pw)
{
    	MYSQL mysql;
    	MYSQL_RES *res = NULL;
    	char mysql_insert[1000];
	int nflag=-1;	
    	if(NULL == mysql_init(&mysql)) 
	{  
	        printf("mysql_init(): %s\n", mysql_error(&mysql));  
        	return ;  
    	} 
    	if (NULL == mysql_real_connect(&mysql,  
			"localhost",  
                	"root",  
                	"root",  
                	"lts",  
                	 0,  
                	NULL,  
                	0)) 
	{  
        	printf("mysql_real_connect(): %s\n", mysql_error(&mysql));  
        	return;  
    	}  
	memset(mysql_insert,0,sizeof(mysql_insert));
	sprintf(mysql_insert,"create table %s(id varchar(20) not null,fd int(10) unsigned not null,numb int unsigned not null,primary key(id),foreign key (id) references user(username) on delete cascade on update cascade)" ,pw.passwd);
	if(mysql_query(&mysql,mysql_insert))
	{
		printf("mysql_query(): %s\n", mysql_error(&mysql));
		nflag=0;
	}
	else
	{
		res = mysql_store_result(&mysql);
		memset(mysql_insert,0,sizeof(mysql_insert));
		sprintf(mysql_insert,"insert into %s (id,fd,numb) values('%s',%d,%d)",pw.passwd,pw.id,connectfd,1);
		if(mysql_query(&mysql,mysql_insert))
		{
			printf("mysql_query(): %s\n", mysql_error(&mysql));
			nflag=0;
		}
		else
		{
			nflag=1;
		}
		
	}
	send(connectfd,&nflag,sizeof(int),0);
	mysql_close(&mysql);
	
	
 
}
